package com.dongdongshop.controller;

import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.CategoryService;
import com.dongdongshop.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("category")
public class CategoryController {

    @DubboReference
    private CategoryService cs;

    @RequestMapping("show")
    public String show(){
        return "admin/content_category";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public Result getAll(String name){
        try {
            List<TbContentCategory> all = cs.getAll(name);
            return Result.OK().setData(all);
        } catch (Exception e) {
            return Result.ERROR();
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbContentCategory cate){
        try {
            cs.insert(cate);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result getById(Long id){
        try {
            TbContentCategory byId = cs.getById(id);
            return Result.OK().setData(byId);
        } catch (Exception e) {
            return Result.response(ResultEnum.GETBYID_ERROR);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbContentCategory cate){
        try {
            cs.update(cate);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("deleteCheck")
    @ResponseBody
    public Result deleteCheck(Long[] ids){
        try {
            cs.deleteCheck(ids);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
}
